package com.jobsys.dbprocess;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jobsys.model.MJobinfo;
import com.jobsys.utils.MyStringUtils;

/**
 * 表market操作
 * 
 * @author pc-12
 *
 */
public class Dbjob {
	/**
	 * 获取参数
	 * 
	 * @param name
	 * @return
	 */
	public static List<MJobinfo> getJobinfos(String typename, String queryStr, int curpage, int pageSize, String scity,
			String sindustry, String stime, String ssource) {
		List<MJobinfo> params = null;

		StringBuilder sql = new StringBuilder();
		sql.append("select * from jobinfo where isDelete=0 ");
		if (null != typename && typename.equals("校园招聘")) {
			sql.append(String.format("and type=%s ", "'" + typename + "'"));
		} else {
			sql.append(String.format("and type<>%s ", "'校园招聘'"));
		}

		if (null != queryStr) {
			sql.append(String.format("and (title like %s or company like %s ) ", "'%" + queryStr + "%'",
					"'%" + queryStr + "%'"));
		}

		if (null != scity) {
			sql.append(String.format("and city like %s ", "'%" + scity + "%'"));
		}

		if (null != sindustry) {
			sql.append(String.format("and industry like %s ", "'%" + sindustry + "%'"));
		}

		if (null != stime && stime != "") {
			sql.append(String.format("and publishtime>=%s ", "'" + stime + "'"));
		}

		if (null != ssource) {
			sql.append(String.format("and source like %s ", "'%" + ssource + "%'"));
		}

		sql.append(String.format(" order by publishtime desc limit %d,%d", (curpage - 1) * pageSize, pageSize));

		params = MJobinfo.dao.find(sql.toString());

		return params;
	}

	/**
	 * 获取总商品数 queryStr,curpage,scity,sindustry,stime,ssource
	 * 
	 * @param source
	 * @return
	 */
	public static int getTotalNums(String typename, String queryStr, String scity, String sindustry, String stime,
			String ssource) {
		int count = 0;
		StringBuilder sql = new StringBuilder();
		sql.append("select count(*) as count from jobinfo where isDelete=0 ");
		if (null != typename && typename.equals("校园招聘")) {
			sql.append(String.format("and type=%s ", "'" + typename + "'"));
		} else {
			sql.append(String.format("and type<>%s ", "'校园招聘'"));
		}

		if (null != queryStr) {
			sql.append(String.format("and (title like %s or company like %s ) ", "'%" + queryStr + "%'",
					"'%" + queryStr + "%'"));
		}

		if (null != scity) {
			sql.append(String.format("and city like %s ", "'%" + scity + "%'"));
		}

		if (null != sindustry) {
			sql.append(String.format("and industry like %s ", "'%" + sindustry + "%'"));
		}

		if (null != stime) {
			sql.append(String.format("and publishtime>=%s ", "'" + stime + "'"));
		}

		if (null != ssource) {
			sql.append(String.format("and source like %s ", "'%" + ssource + "%'"));
		}

		MJobinfo model = MJobinfo.dao.findFirst(sql.toString());
		count = model.getLong("count").intValue();

		return count;
	}

	/**
	 * 插入数据
	 * 
	 * @param model
	 * @return
	 */
	public static boolean insert(MJobinfo model) {
		MJobinfo.dao = model;

		int busicode = 1;
		switch (MJobinfo.dao.getStr("source")) {
		case "猎聘网":
			busicode = 1;
			break;
		case "前程无忧":
			busicode = 2;
			break;
		case "智联招聘":
			busicode = 3;
			break;
		default:
			busicode = 4;
			break;
		}

		MJobinfo.dao.set("keyID", MyStringUtils.keyIdFactory(busicode));
		if (MJobinfo.dao.get("addTime") == null) {
			MJobinfo.dao.set("addTime", new Date());
		}
		MJobinfo.dao.save();
		return true;
	}

	/**
	 * 判重
	 * 
	 * @param detailLink
	 * @param source
	 * @return
	 */
	public static boolean isExist(String detailLink) {
		MJobinfo result = new MJobinfo();
		result = MJobinfo.dao.findFirst(
				"select Count(*) as count from jobinfo where isDelete=0 and titlelink=? limit 1", detailLink);

		int nums = result.getLong("count").intValue();
		if (nums > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除数据
	 * 
	 * @param time
	 */
	public static void delete(String time) {
		List<Record> records = Db.find("select keyID from jobinfo where publishtime<?", time);

		for (int i = 0; i < records.size(); i++) {
			Db.delete("jobinfo", "keyID", records.get(i));
		}
	}

	// 统计汇总
	public static List<MJobinfo> CountAll() {
		List<MJobinfo> result = MJobinfo.dao.find(
				"select count(*) as num,publishtime from jobinfo where isDelete=0 GROUP BY publishtime order by publishtime asc");

		return result;
	}
}
